package com.pet.shop.controller.user;

import com.pet.config.SessionConstants;
import com.pet.shop.entity.user.SysUser;
import com.pet.shop.service.user.ISysUserService;
import com.pet.shop.utils.ApiResultUtil;
import com.pet.shop.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by xiaochanzi on 2018/3/15.
 */
@RestController
@RequestMapping("/web/payload/")
public class WebLoginController {

    private static final Logger logger = LoggerFactory.getLogger(WebLoginController.class);

    @Autowired
    private ISysUserService sysUserService;

    /**
     *
     * @param request
     * @param payload
     * @return
     */
    @RequestMapping("login")
    public Map<String, Object> login(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> payload){
        Cookie cookie = new Cookie("JSESSIONID", request.getSession().getId());
        response.addCookie(cookie);
        String loginName = payload.get("loginName");
        String verifyCode = payload.get("verifyCode");
        String password = payload.get("password");
        if(StringUtils.isEmpty(loginName)){
            return ApiResultUtil.error_param("用户名是必须传输参数");
        }
        SysUser sysUser = sysUserService.queryByLoginName(loginName);
        if(sysUser == null){
            return ApiResultUtil.error_param("用户不存在");
        }
        Object code = request.getSession().getAttribute("SMS_CODE");
        if(code != null && !code.equals(verifyCode)){
            if(password == null || !sysUser.getPassword().equals(MD5Util.md5(password))){
                return ApiResultUtil.error_param("用户名或者密码错误");
            }else{
                return ApiResultUtil.error_param("验证码错误");
            }
        }

        if(sysUser.getIsLogin() == null || sysUser.getIsLogin() != 1){
            return ApiResultUtil.error_param("此用户不允许登录");
        }
        sysUser.setPassword(null);

        //更新最新登录时间
        SysUser sysUser2 = new SysUser();
        sysUser2.setId(sysUser.getId());
        sysUser2.setLastLoginTime(new Date());
        sysUserService.updateEntity(sysUser2);
        request.getSession().setAttribute(SessionConstants.USER_SESSION_KEY, sysUser);
        logger.info("---- {} 登录成功 ----", sysUser.getLoginName());
        return ApiResultUtil.success(sysUser);
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @RequestMapping("logout")
    public Map<String, Object> login(HttpServletRequest request){
        SysUser sysUser = (SysUser) request.getSession().getAttribute(SessionConstants.USER_SESSION_KEY);
        if(sysUser != null){
            logger.info("---- {} 登出 ----", sysUser.getLoginName());
        }

        request.getSession().removeAttribute(SessionConstants.USER_SESSION_KEY);
        return ApiResultUtil.success(null);
    }

    @RequestMapping("test")
    public Map<String, Object> test(){
        return ApiResultUtil.success("success");
    }
}
